/*
 * @Author: zhangyun
 * @Date: 2021-04-27 09:34:51
 * @LastEditors: zhangyun
 * @LastEditTime: 2021-04-27 10:45:08
 * @Desc:
 */
const express = require('express')
const router = express.Router()
const _ = require('lodash')
const ideaModel = require('../model/ideaModel')
const message = require('../utils/message')

router.get('/', (req, res) => {
  const { user } = req.query
  ideaModel
    .find({ user: user })
    .then((result) => {
      res.status(200).send(message(200, '查询成功', result))
    })
    .catch((err) => {
      console.log('idea list', err)
      res.status(400).send(message(400, '查询失败'))
    })
})

router.post('/', (req, res) => {
  const { user, idea } = req.body
  console.log(user, idea)
  if (_.isEmpty(user)) return res.status(400).send(message(400, '用户名称不能为空'))
  if (_.isEmpty(idea)) return res.status(400).send(message(400, '编辑内容'))
  ideaModel
    .insertMany({ user: user, idea: idea })
    .then(() => {
      res.status(200).send(message(200, '创建成功'))
    })
    .catch((err) => {
      console.log('idea create', err)
      res.status(400).send(message(400, '创建失败'))
    })
})

router.delete('/', (req, res) => {
  const { id } = req.query
  ideaModel
    .findById(id)
    .deleteOne()
    .then((result) => {
      res.status(200).send(message(200, '删除成功'))
    })
    .catch((err) => {
      console.log('idea delete', err)
      res.status(400).send(message(400, '删除失败'))
    })
})

module.exports = router
